package com.da.web.sys_login.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.da.common.Result;
import com.da.web.sys_login.entity.LoginParm;
import com.da.web.sys_login.entity.UpdateVo;
import com.da.web.sys_login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * @author da
 * @time 2023/12/4 上午 9:32
 */
@RestController
@RequestMapping("/api/login")
public class LoginController {
    @Autowired
    private LoginService loginService;


    //    生成验证码
    @SaIgnore
    @GetMapping("/code")
    public void getCode(HttpServletRequest request, HttpServletResponse response) {
        loginService.getCode(request, response);
    }

    //获取所有的角色类型
    @SaIgnore
    @GetMapping("/roleTypes")
    public Result getRoleTypes() {
        return loginService.getRoleTypes();
    }

    //允许直接访问的接口
    @SaIgnore
    @PostMapping
    public Result login(@RequestBody LoginParm parm, HttpServletRequest request) {
        return loginService.login(parm, request);
    }

    //退出登录
    @GetMapping("/logout")
    public Result logout() {
        StpUtil.logout();
        return Result.success("退出成功");
    }

    //获取用户信息
    @GetMapping("/getInfo")
    public Result getInfo(Long userId, String userType) {
        return loginService.getInfo(userId, userType);
    }

    //查询菜单
    @GetMapping("/getMenuList")
    public Result getMenuList(Long userId, String userType) {
        return loginService.getMenuList(userId, userType);
    }

    //更新用户的头像、名字、密码
    @PutMapping("/updateInfo")
    public Result updateInfo(@RequestBody UpdateVo userInfo) {
        return loginService.updateInfo(userInfo);
    }
}
